package com.agd.quick.states;
//Op
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;
import lib.elementgame.GameSprite;
import lib.elementgame.Mfx;
//import lib.elementgame.Sfx;
import lib.engine.Anchor;
import lib.engine.GameEngine;
import lib.engine.GameState;

public class StateOption extends GameState {
	private static GameSprite backgroundFloor;
	private static GameSprite background;
	private static GameSprite background2;
	private static GameSprite background3;
	
	private GameSprite[] Option_mute = new GameSprite[2];
	private GameSprite[] Option_on = new GameSprite[2];;
//	private GameText Title_Option;

	private boolean keluar;
	public StateOption(GameEngine engine) {
		super(engine);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		if (pSceneTouchEvent.isActionUp()) {
			if (pTouchArea == Option_on[0] && Option_on[0].isVisible()) {
				Mfx.Pause(BG_SOUND);
				
			}
			else if (pTouchArea == Option_mute[0] && Option_mute[0].isVisible()) {
				if (Mfx.isPaused(BG_SOUND)) {
					Mfx.Play(BG_SOUND);
					
				}
			}
			// untuk sound
			else if (pTouchArea == Option_mute[1]) {
				if(Option_mute[1].isVisible()){
				Option_mute[1].setVisible(false);
				Option_on[1].setVisible(true);
				engine.vibrate(400);
				}
				else{
					Option_mute[1].setVisible(true);
					Option_on[1].setVisible(false);
				}
			}
			
		}
		return false;
	}

	@Override
	public void initComponent() {
		// TODO Auto-generated method stub
		background = new GameSprite(GP_SPRITE_BACKGGROUND, engine);
		background2 = new GameSprite(GP_SPRITE_BACKGGROUND2, engine);
		background3 = new GameSprite(GP_SPRITE_BACKGGROUND3, engine);
		backgroundFloor = new GameSprite(OP_SPRITE_BG, engine);
		Option_mute[0] = new GameSprite(OP_SPRITE_MUTE, engine);
		Option_on[0] = new GameSprite(OP_SPRITE_PLAY, engine);
		Option_mute[1] = new GameSprite(OP_SPRITE_MUTE, engine);
		Option_on[1] = new GameSprite(OP_SPRITE_PLAY, engine);
//		Title_Option = new GameText("OPTION", 20, engine.getFont(FONT_OPTION), engine);

	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		// untuk musik latar
		keluar =false;
		if (Integer.parseInt(engine.getDatabase().getData(SETTING_SOUND, 0, 1)) == 1) {
			Option_mute[0].setVisible(false);
			Option_on[0].setVisible(!false);
		} else {
			Option_mute[0].setVisible(!false);
			Option_on[0].setVisible(false);
		}
		// untuk mengatur vibrator
		if(Integer.parseInt(engine.getDatabase().getData(SETTING_VIBRATION, 0, 1)) == 0){
			Option_mute[1].setVisible(true);
			Option_on[1].setVisible(false);
		}
		else{
			Option_mute[1].setVisible(!true);
			Option_on[1].setVisible(!false);
		}
		background.setWidth(800);
		background.setHeight(550);
		background2.setWidth(800);
		background2.setHeight(450);
		background3.setWidth(800);
		background3.setHeight(450);
		backgroundFloor.setWidth(800);
	}

	@Override
	protected void attach() {
		// TODO Auto-generated method stub
		engine.scene.attachChild(background3);
		engine.scene.attachChild(background2);
		engine.scene.attachChild(background);
		engine.scene.attachChild(backgroundFloor);
		engine.scene.attachChild(Option_mute[0]);
		engine.scene.attachChild(Option_on[0]);
		engine.scene.attachChild(Option_mute[1]);
		engine.scene.attachChild(Option_on[1]);
//		engine.scene.attachChild(Title_Option);

	}

	@Override
	protected void detach() {
		// TODO Auto-generated method stub
		background3.detachSelf();
		background2.detachSelf();
		background.detachSelf();
		backgroundFloor.detachSelf();
		Option_mute[0].detachSelf();
		Option_on[0].detachSelf();
		Option_mute[1].detachSelf();
		Option_on[1].detachSelf();
//		Title_Option.detachSelf();

	}

	@Override
	protected void setPosition() {
		// TODO Auto-generated method stub
		background.setPosition(Anchor.BOTTOM_LEFT);
		background2.setX(0);
		background2.setY(-20);
		background3.setX(0);
		background3.setY(-50);
		Option_on[0].setPosition(30, -135, Anchor.CENTER_LEFT);
		Option_mute[0].setPosition(30, -135, Anchor.CENTER_LEFT);
		Option_on[1].setPosition(30, -10, Anchor.CENTER_LEFT);
		Option_mute[1].setPosition(30, -10, Anchor.CENTER_LEFT);
//		Title_Option.setPosition(Anchor.TOP_CENTER);
	}

	@Override
	protected void registerTouch() {
		// TODO Auto-generated method stub
		engine.scene.registerTouchArea(Option_mute[0]);
		engine.scene.registerTouchArea(Option_on[0]);
		engine.scene.registerTouchArea(Option_mute[1]);
		engine.scene.registerTouchArea(Option_on[1]);
	}

	@Override
	protected void unregisterTouch() {
		// TODO Auto-generated method stub
		engine.unregisterSceneTouch(Option_mute[0]);
		engine.unregisterSceneTouch(Option_on[0]);
		engine.unregisterSceneTouch(Option_mute[1]);
		engine.unregisterSceneTouch(Option_on[1]);
	}

	@Override
	protected void onUpdate() {
		// TODO Auto-generated method stub
		if (!Mfx.isPaused(BG_SOUND)) {
			Option_mute[0].setVisible(false);
			Option_on[0].setVisible(!false);
		} else {
			Option_mute[0].setVisible(!false);
			Option_on[0].setVisible(false);
		}
		// untuk sound (pilih menu menabrak item, dll)
		
		if(keluar){
			exitState(MainMenuState);
		}
	}

	@Override
	protected void onPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onResumed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if (Option_mute[1].isVisible()) {
				engine.getDatabase().updateData(SETTING_VIBRATION, new int[]{1}, new String[]{"" + 0}, "WHERE id_SET1 = 0");
			} else {
				engine.getDatabase().updateData(SETTING_VIBRATION, new int[]{1}, new String[]{"" + 1}, "WHERE id_SET1 = 0");
			}
			if (!Mfx.isPaused(BG_SOUND)) {
				engine.getDatabase().updateData(SETTING_SOUND, new int[]{1}, new String[]{"" + 1}, "WHERE id_SET2 = 0");
			} else {
				engine.getDatabase().updateData(SETTING_SOUND, new int[]{1}, new String[]{"" + 0}, "WHERE id_SET2 = 0");
			}
			keluar = true;
		}
	}

	@Override
	public void initLoading() {
		// TODO Auto-generated method stub
		
	}

}
